package com.flca.dbs.berkeley;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

import com.flca.frw.IEnum;
import com.flca.frw.util.TransformUtils;

public class BerkeleyIOUtils {

	public static TransformUtils tu = TransformUtils.getInstance();
	
//	public static long toPk(IEntity aEntity) 
//	{
//		if (aEntity != null) {
//			return toLong((Long) aEntity.getPk()); 
//		} else {
//			return Long.MIN_VALUE;
//		}
//	}
	
	public static Long toLong(String aValue) 
	{
		Long result = null;
		if (aValue != null) {
			result = new Long(aValue);
			if (result == Long.MIN_VALUE) {
				result = null;
			}
		}
		return result;
	}


	public static Double toDouble(String aValue) 
	{
		Double result = null;
		if (aValue != null) {
			result = new Double(aValue);
			if (result == Double.MIN_VALUE) {
				result = null;
			}
		}
		return result;
	}
	
	public static BigDecimal toBigDecimal(String aValue) 
	{
		if (aValue != null) {
			return new BigDecimal(aValue);
		} else {
			return null;
		}
	}

	public static Date toDate(String aValue) {
		if (aValue != null) {
			try {
				Date result = new Date();
				result.setTime(new Long(aValue));
				return result;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return null;
		}
	}
	
	public static Timestamp toTimestamp(String aValue) {
		Long l = toLong(aValue);
		if (l != null) {
			return toTimestamp(l);
		} else {
			return null;
		}
	}
	
	public static Timestamp toTimestamp(long aValue) {
		if (aValue != Long.MIN_VALUE) {
			return new Timestamp(aValue);
		} else {
			return null;
		}
	}
	
	public static Boolean toBoolean(String aValue) {
		return tu.string2Boolean(aValue);
	}
	
	//--- convert -> String
	
	public static String toString(Long aValue) 
	{
		if (aValue == null) {
			return null;
		} else {
			return "" + aValue;
		}
	}
	
	public static String toString(Date aValue) 
	{
		if (aValue == null) {
			return null;
		} else {
			return "" + aValue.getTime();
		}
	}

	public static String toString(Timestamp aValue) 
	{
		if (aValue == null) {
			return null;
		} else {
			return "" + aValue.getTime();
		}
	}

	public static String toString(Double aValue) 
	{
		if (aValue == null) {
			return null;
		} else {
			return "" + aValue.doubleValue();
		}
	}
	
	public static String toString(BigDecimal aValue) 
	{
		if (aValue != null) {
			return tu.bigDecimal2String(aValue);
		} else {
			return null;
		}
	}

	public static String toString(Boolean aValue) 
	{
		if (aValue != null) {
			return tu.boolean2String(aValue);
		} else {
			return null;
		}
	}

	public static String toString(Object aValue) 
	{
		try {
			return tu.object2String(aValue);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String toString(IEnum aValue) 
	{
		if (aValue == null) {
			return null;
		} else {
			return aValue.toString(); //TODO
		}
	}

	public static String toString(String aValue)
	{
		return aValue;
	}

}
